package com.jinlingxiu.app.other.db.database

import android.content.Context
import androidx.room.Database
import androidx.room.Room
import androidx.room.RoomDatabase
import com.jinlingxiu.app.other.db.dao.AnswerDao
import com.jinlingxiu.app.other.db.entity.AnswerBean


@Database(
    entities = [AnswerBean::class],
    version = 1
)
abstract class AssetsDataManager : RoomDatabase() {

    abstract fun unitDao(): AnswerDao

    companion object {
        private const val DB_NAME: String = "answer.db"

        @Volatile
        private var INSTANCE: AssetsDataManager? = null

        fun getInstance(context: Context): AssetsDataManager =
            INSTANCE ?: synchronized(this) {
                INSTANCE ?: buildDatabase(context).also { INSTANCE = it }
            }

        private fun buildDatabase(context: Context) =
            Room.databaseBuilder(
                context.applicationContext, AssetsDataManager::class.java, DB_NAME
            ).createFromAsset(DB_NAME).allowMainThreadQueries().build()
    }
}